package priv.winnine.entity.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import priv.winnine.entity.enums.ResultEnum;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response {

    private Map<String, Object> info = new HashMap<>();

    private Map<String, Object> subject = new HashMap<>();

    private Map<String, Object> result = new HashMap<>();

    private String timestamp;

    private String data;

    public Response(Integer code, String description) {
        result.put("code", code);
        result.put("description", description);
    }

    public Response addInfo(String key, Object value) {
        this.info.put(key, value);
        return this;
    }

    public Response addSubject(String key, Object value) {
        subject.put(key, value);
        return this;
    }

    public Response addResult(String key, Object value) {
        result.put(key, value);
        return this;
    }

    public Response addData(String data){
        this.data = data;
        return this;
    }

    public Boolean ifSuccess() {
        Integer code = (Integer) result.get("code");
        return code.equals(ResultEnum.SUCCESS.getCode());
    }

    @SneakyThrows
    @Override
    public String toString() {
        return new ObjectMapper().writeValueAsString(this);
    }

}
